package com.chanyachting.emall.serviceImpl;

import java.util.List;

import com.chanyachting.emall.dao.AdminDAO;
import com.chanyachting.emall.domain.Admin;
import com.chanyachting.emall.service.AdminService;

public class AdminServiceImpl implements AdminService {
	
	private AdminDAO adminDao;

	public AdminServiceImpl() {
		
		adminDao=new AdminDAO();
	}

	public boolean createAdmin(Admin admin) {
		
		adminDao.save(admin);
		
		return true;
	}

	public boolean delAdmin(Admin admin) {
		
		this.adminDao.delete(admin);
		
		return true;
	}

	public Admin login(Admin admin) {
		
		if(this.adminDao.checkAdminAccountPassword(admin)){
			
			
			String args[]={admin.getAccount(),admin.getPassword()};
			
			List list=(List)this.adminDao.find("from Admin where account=? and password=?",args);
			
			if(list!=null && list.size()>0){
				
				Admin tempAdmin=(Admin)list.get(0);
				
				//检查会员类型，如果不是战略管理员不能登录
				//if(!tempAdmin.getType().equals("01")) return null;
				
				tempAdmin.setLoginDate(admin.getLoginDate());
				tempAdmin.setLoginIP(admin.getLoginIP());
				
				this.adminDao.update(tempAdmin);
				
				return tempAdmin;
				
			}
			
			
		}
		
		return null;
	}

	public boolean updatePassword(Admin admin, String newPassword) {
		
		if(this.adminDao.checkAdminAccountPassword(admin)){
			
			admin.setPassword(newPassword);
			
			this.adminDao.update(admin);
			
			return true;
		}
		
		return false;
	}

}
